package com.atguigu.gulimall.order.service.impl;

import com.atguigu.gulimall.order.entity.User;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author wangzhenjun
 * @date 2022/3/23 11:50
 */
@Service
public class TestService {

    @RabbitListener(queues = {"hello-java-queue"})
    public void reciveMessage(Message message, User user, Channel channel){

        System.out.println("接收的信息:" + user);
        // channel内按顺序自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag===>" + deliveryTag);
        // 签收 ，非批量模式
        try {
            if (deliveryTag % 2 == 0){
                channel.basicAck(deliveryTag,false);
                System.out.println("货物签收");
            }else {
                // 第三个参数：true退回，false丢弃
                channel.basicNack(deliveryTag,false,true);
                System.out.println("拒绝签收");
            }
        } catch (IOException e) {
            // 断网
            e.printStackTrace();
        }
    }
}
